/*********************
* Author:Dzlua
* QQ:505544956
* time:2016/05/04
*/
#ifndef __EDUI_CONTROL_H__
#define __EDUI_CONTROL_H__

#include "../core/Msg.h"
#include "../core/Base.h"
#include "../core/Event.h"
#include "../utilities/String.h"
#include "../external/tinyxml/tinyxml.h"

#include <vector>

EDUI_NAMESPACE_BEGIN

class IWindow;
class IRender;
class IFont;
class Container;
class ICursor;
class Animation;

struct EDUI_API ImageData
{
    String img;
    String mask;
    Rect* src;
    Rect* dst;
    Rect1* cor;
    bool hole;
    bool center;

    ImageData();
    ~ImageData();
    void Reset(const char* _img, Rect* _src, Rect* _dst, Rect1* _cor, bool* _hole, bool* _center, const char* _mask = 0);
    void Reset(ImageData* imgdata);
};

class EDUI_API Control
{
    friend class IEngine;
    friend class IWindow;
    friend class Container;
    friend class TableLayout;
protected:
    Control();
    virtual ~Control();
public:
#ifdef EDUI_TEST_NEW_DELETE
    static int m_newdeleteNum;
    static int _GetNewDeleteNum();
#endif
	static Control* GetInterface();
	virtual void Release();
    bool IsOK() const;
public:
    //control name
    const char* GetName() const;
    bool SetName(const char* name);

    // render position
    virtual Point GetPos() const;
    virtual bool  SetPos(const Point* pt);
    virtual Size  GetSize() const;
    virtual bool  SetSize(const Size* sz);
    virtual Rect  GetRect() const;
    virtual bool  SetRect(const Rect *rc);
    // real position
    virtual Point GetFixedPos() const;
    virtual bool  SetFixedPos(const Point* pt);
    virtual Size  GetFixedSize() const;
    virtual bool  SetFixedSize(const Size* sz);
    virtual Rect  GetFixedRect() const;
    virtual bool  SetFixedRect(const Rect *rc);

    virtual Rect1 GetPadding() const;
    virtual bool  SetPadding(const Rect1* padding);

    //user attribute
    void* GetUserData() const;
	void* SetUserData(void* data);
    //
    bool IsFloat() const;
    void SetFloat(bool bFloat);
    bool IsVisible() const;
    virtual void SetVisible(bool bVisible);
    bool IsEnabled() const;
    void SetEnabled(bool bEnable);
    bool IsMouseEnabled() const;
    void SetMouseEnabled(bool bEnable);
    bool IsKeyboardEnabled() const;
    void SetKeyboardEnabled(bool bEnable);
    bool IsFocused() const;
    void SetFocus();
	void KillFocus();

    bool IsDragWindow() const;
    bool IsInRect(int x, int y);

    IWindow* GetWindow() const;
    Control* GetParent() const;
    // rc in rc
    bool NeedRender(const Rect* rc);
    void Invaldate(const Rect* rc = 0);

    bool LoadLayout(const char* xmlstr);
    bool LoadLayout(TiXmlElement* root);

    void SetCursor(ICursor* cursor);
    ICursor* GetCursor();

    /*
        "dst:hottext,pressedtext;color:#FF00FFFF,#FFFFFFFF;"
            : hottext=#FF00FFFF, pressedtext=#FFFFFFFF
        "dst:hottext.pressedtext;color:#FFFFFFFF;"
            : hottext=#FFFFFFFF, pressedtext=#FFFFFFFF
    */
    void SetColor(const char* colorstr);
    // text,bk,hottext,...
    const Color* GetColor(const char* dstname);

    void SetImage(const char* imagestr);
    void SetStatusImage(const char* statusimagestr);
    const ImageData* GetImage(const char* dstname);

    virtual void AutoLayout();

    virtual Control* FindControl(const char* name);
    virtual Control* FindControlByTabID(int id);
    virtual Control* FindControlByPt(int x, int y, Uint32 flag = FIND_ALL);
    virtual Control* FindParent(const char* name);

    void SetTabID(int id);
    int GetTabID() const;

    void SetAnimation(Animation* animation, bool start = true);
    Animation* GetAnimation();
protected:
    //msg
    virtual bool HandleMsg(Msg* msg);
    //paint
    virtual void Render(IRender* render, const Rect* rc);
    virtual void RenderBkColor(IRender* render, const Rect* rc);
    virtual void RenderBkImages(IRender* render, const Rect* rc);
    virtual void RenderStatusImages(IRender* render, const Rect* rc);
    virtual void RenderForeImages(IRender* render, const Rect* rc);
    virtual void RenderText(IRender* render, const Rect* rc);
    virtual void RenderBorder(IRender* render, const Rect* rc);
    
    virtual void SetAttribute(const char* name, const char* value);
    virtual void AfterSetAttribute();

    virtual Color* GetDestColor(const char* dstname);
    virtual ImageData* GetDestImageData(const char* dstname);

    virtual void DoEvent(EventType type, void* data1 = 0, void* data2 = 0);
protected:
    bool SetWindow(IWindow* wnd);
    bool SetParent(Control* ctrl);

    Rect GetImageDrawRect(const Rect* imgRc, const Rect1* rcInset = 0);

    void DrawImage(IRender* render, ImageData* image, const Rect1* rcInset = 0);
    void DrawImage(IRender* render, ImageData* image, const Rect* rc);
protected:
    virtual void OnFocus();
    virtual void OnKillFocus();
    virtual void OnMouseIn();
    virtual void OnMouseOut();
protected:
    String m_name;

    Rect m_rc;
    Rect m_rcFixed;
    Rect1 m_rcPadding;

    bool m_bFloat;
    bool m_bVisible;
    bool m_bEnabled;
    bool m_bMouseEnabled;
    bool m_bKeyboardEnabled;
    bool m_bDragWnd;

    Control* m_parent;
    IWindow* m_wnd;

    Color m_bkColor;

    ImageData m_bkImage;
    ImageData m_foreImage;

    ICursor* m_cursor;

    int m_tabID;
    Animation* m_ainmation;
};

EDUI_NAMESPACE_END

#endif
